package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.annotation.OperateLog;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookCategoryOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocCategoryDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.VocCategoryCommand;
import com.cskaoyan.wordmemorize.service.VocCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/voc")
public class VocCategoryController {

    @Autowired
    VocCategoryService vocCategoryService;

    @GetMapping("category")
    public Result getBookCategory(Long categoryId){
        VocCategoryDTO bookCategory = vocCategoryService.getBookCategory(categoryId);
        if (bookCategory == null){
            return Result.fail("查找失败，没有此书");
        }
        return Result.ok(bookCategory);
    }

    @PostMapping("category")
    @OperateLog(dateType = "书记类目", operateType = 1)
    public Result addBookCategory(@RequestBody
                                      VocCategoryCommand command){

        // 参数校验
        if (command == null)
            return Result.fail("添加失败");
        Long aLong = vocCategoryService.addBookCategory(command);
        if (aLong == 0L){
            return Result.fail("添加失败");
        }
        return Result.ok();
    }

    @PutMapping("category")
    public Result updateBookCategory(@RequestBody
                                         VocCategoryCommand command){
        // 参数校验
        if (command == null)
            return Result.fail("更新失败");
        vocCategoryService.updateBookCategory(command);
        return Result.ok();
    }

    @GetMapping("category/list")
    public Result getBookCategories(PageRequest pageQuery){
        PageDTO<VocCategoryDTO> bookCategories = vocCategoryService.getBookCategories(pageQuery);

        // 参数校验
        if (bookCategories == null){
            return Result.fail("没有数据");
        }
        return Result.ok(bookCategories);
    }

    @DeleteMapping("category/{categoryId}")
    public Result deleteBookCategory(@PathVariable Long categoryId){

        // 参数校验
        if (categoryId == null)
            return Result.fail("删除失败,书籍id不能为空");
        DeleteBookCategoryOperateDTO deleteBookCategoryOperateDTO = vocCategoryService.deleteBookCategory(categoryId);
        if (deleteBookCategoryOperateDTO == null){
            return Result.fail("删除失败，查完此书籍");
        }
        return Result.ok(deleteBookCategoryOperateDTO);
    }

    @DeleteMapping("categories")
    public Result deleteBookCategories(@RequestBody BatchDeleteCommand command){
        // ids 前端界面有校验，就不检测了
        List<DeleteBookCategoryOperateDTO> deleteBookCategoryOperateDTOList = vocCategoryService.deleteBookCategories(command);
        if (deleteBookCategoryOperateDTOList == null){
            return Result.fail("批量删除失败");
        }
        return Result.ok(deleteBookCategoryOperateDTOList);
    }
}
